#include "rootuser.h"

Rootuser::Rootuser(std::string username)
{
    this->username = username;
}

int Rootuser::drawLoginStart()
{
    int _max = drawLogin_Info();
    drawLoginMenu(_max);
    std::string strret;
    strret = readCommand();
    int ret = isDigit(strret);
    if(ret != 1 && ret != 2 && ret != 3 && ret != 4) return drawLoginStart();
    else return ret;
    return 0;
}

int Rootuser::drawStudentStart()
{
    int _max = drawStudent_Info();
    drawStudentMenu(_max);
    std::string strret;
    strret = readCommand();
    int ret = isDigit(strret);
    if(ret < 1 || ret > 6) return drawStudentStart();
    else return ret;
    return 0;
}

int Rootuser::drawSubjectStart()
{
    int _max = drawSubject_Info();
    drawSubjectMenu(_max);
    std::string strret;
    strret = readCommand();
    int ret = isDigit(strret);
    if(ret < 1 || ret > 6) return drawSubjectStart();
    else return ret;
    return 0;
}

void Rootuser::drawLoginMenu(int _max)
{
    int peice[2];
    peice[0] = (_max - 2) / 2 - 1;
    peice[1] = (_max - 2) - (_max - 2) / 2;
    std::vector<std::string> menu_list, tail_list;
    std::cout << "\033[01;35m|\033[0m";
    for (int j = 0; j < ((_max - 2) / 2) - (9 / 2); j++) std::cout << " ";
    std::cout << "\033[01;35m" << "Main menu" << "\033[0m";
    for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (9 - 9 / 2); j++) std::cout << " ";
    std::cout << "\033[01;35m|\033[0m" << std::endl;
    for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    menu_list.push_back("1.Add account");
    menu_list.push_back("2.Delete account");
    menu_list.push_back("3.Back");
    menu_list.push_back("4.Exit System");
    for (int i = 0; i < menu_list.size(); i++)
    {
        std::cout << "\033[01;35m|\033[0m";
        for(int j = 0; j < (peice[i%2]/2)-(menu_list[i].size()/2); j++) std::cout << " ";
        std::cout << "\033[01;31m" << menu_list[i] << "\033[0m";
        for(int j = 0; j < (peice[i%2]-peice[i%2]/2)-(menu_list[i].size()-menu_list[i].size()/2); j++) std::cout << " ";
        if(i == 1 || i == 3)
        {
            std::cout << "\033[01;35m|\033[0m";
            std::cout << std::endl;
            if(i == 1) 
            {
                for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
                std::cout << std::endl;
            }
        }
    }
    

    for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    tail_list.push_back("Your choice[1/2/3/4]:");
    tail_list.push_back("Press Enter to continue...");
    for(int i = 0; i < tail_list.size(); i++)
    {
        std::cout << "\033[01;35m|\033[0m";
        std::cout << "\033[01;31m" << tail_list[i] << "\033[0m";
        for (int j = 0; j < ((_max - 2) - tail_list[i].size()); j++) std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
        for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }
    moveCursor('u', 4);
    moveCursor('r', 22);

}

void Rootuser::drawStudentMenu(int _max)
{
    int peice[2];
    peice[0] = (_max - 2) / 2 - 1;
    peice[1] = (_max - 2) - (_max - 2) / 2;
    std::vector<std::string> menu_list, tail_list;
    std::cout << "\033[01;35m|\033[0m";
    for (int j = 0; j < ((_max - 2) / 2) - (9 / 2); j++) std::cout << " ";
    std::cout << "\033[01;35m" << "Main menu" << "\033[0m";
    for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (9 - 9 / 2); j++) std::cout << " ";
    std::cout << "\033[01;35m|\033[0m" << std::endl;
    for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    menu_list.push_back("1.Add Student");
    menu_list.push_back("2.Delete Student");
    menu_list.push_back("3.Modify Student Information");
    menu_list.push_back("4.Check Student Information");
    menu_list.push_back("5.Back");
    menu_list.push_back("6.Exit System");
    for (int i = 0; i < menu_list.size(); i++)
    {
        std::cout << "\033[01;35m|\033[0m";
        for(int j = 0; j < (peice[i%2]/2)-(menu_list[i].size()/2); j++) std::cout << " ";
        std::cout << "\033[01;31m" << menu_list[i] << "\033[0m";
        for(int j = 0; j < (peice[i%2]-peice[i%2]/2)-(menu_list[i].size()-menu_list[i].size()/2); j++) std::cout << " ";
        if(i == 1 || i == 3 || i == 5)
        {
            std::cout << "\033[01;35m|\033[0m";
            std::cout << std::endl;
            if(i == 1 || i == 3) 
            {
                for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
                std::cout << std::endl;
            }
        }
    }
    

    for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    tail_list.push_back("Your choice[1/2/3/4/5/6]:");
    tail_list.push_back("Press Enter to continue...");
    for(int i = 0; i < tail_list.size(); i++)
    {
        std::cout << "\033[01;35m|\033[0m";
        std::cout << "\033[01;31m" << tail_list[i] << "\033[0m";
        for (int j = 0; j < ((_max - 2) - tail_list[i].size()); j++) std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
        for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }
    moveCursor('u', 4);
    moveCursor('r', 26);

}

void Rootuser::drawSubjectMenu(int _max)
{
    int peice[2];
    peice[0] = (_max - 2) / 2 - 1;
    peice[1] = (_max - 2) - (_max - 2) / 2;
    std::vector<std::string> menu_list, tail_list;
    std::cout << "\033[01;35m|\033[0m";
    for (int j = 0; j < ((_max - 2) / 2) - (9 / 2); j++) std::cout << " ";
    std::cout << "\033[01;35m" << "Main menu" << "\033[0m";
    for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (9 - 9 / 2); j++) std::cout << " ";
    std::cout << "\033[01;35m|\033[0m" << std::endl;
    for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    menu_list.push_back("1.Add Subject");
    menu_list.push_back("2.Delete Subject");
    menu_list.push_back("3.Modify Subject Information");
    menu_list.push_back("4.Check Subject Information");
    menu_list.push_back("5.Back");
    menu_list.push_back("6.Exit System");
    for (int i = 0; i < menu_list.size(); i++)
    {
        std::cout << "\033[01;35m|\033[0m";
        for(int j = 0; j < (peice[i%2]/2)-(menu_list[i].size()/2); j++) std::cout << " ";
        std::cout << "\033[01;31m" << menu_list[i] << "\033[0m";
        for(int j = 0; j < (peice[i%2]-peice[i%2]/2)-(menu_list[i].size()-menu_list[i].size()/2); j++) std::cout << " ";
        if(i == 1 || i == 3 || i == 5)
        {
            std::cout << "\033[01;35m|\033[0m";
            std::cout << std::endl;
            if(i == 1 || i == 3) 
            {
                for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
                std::cout << std::endl;
            }
        }
    }
    

    for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;

    tail_list.push_back("Your choice[1/2/3/4/5/6]:");
    tail_list.push_back("Press Enter to continue...");
    for(int i = 0; i < tail_list.size(); i++)
    {
        std::cout << "\033[01;35m|\033[0m";
        std::cout << "\033[01;31m" << tail_list[i] << "\033[0m";
        for (int j = 0; j < ((_max - 2) - tail_list[i].size()); j++) std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
        for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }
    moveCursor('u', 4);
    moveCursor('r', 26);

}

int Rootuser::drawLogin_Info()
{
    system("clear");
    std::vector<std::string> title_list;
    title_list.push_back("Login Manage System");
    title_list.push_back("Login Information");
    file = readFile(LG_PATH);
    
    int max_[4] = {0};

    for (int i = 0; i < file.size(); i++)
    {
        for(int j = 0; j < file[i].size(); j++)
        {
            if(file[i][j].size() > max_[i]) max_[i] = file[i][j].size();
        } 
        max_[i] += 10;
    }

    int _max = 0;
    for(int i = 0; i < 4; i++) _max += max_[i];
    _max += 5;

    for (int i = 0; i < title_list.size(); i++)
    {

        for (int j = 0; j < _max; j++)
            std::cout << "\033[01;35m-\033[0m";

        std::cout << std::endl;
        std::cout << "\033[01;35m|\033[0m";
        for (int j = 0; j < ((_max - 2) / 2) - (title_list[i].size() / 2); j++)
            std::cout << " ";
        std::cout << "\033[01;35m" << title_list[i] << "\033[0m";
        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list[i].size() - title_list[i].size() / 2); j++)
            std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }

    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;
    
    for (int i = 0; i < file[0].size(); i++)
    {
        for(int j = 0; j < file.size(); j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (file[j][i].size() / 2); k++) std::cout << " ";
            
            std::cout << "\033[01;32m" << file[j][i] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (file[j][i].size() - file[j][i].size() / 2); k++)
            std::cout << " ";

            
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;
        for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }
    return _max;
}

int Rootuser::drawStudent_Info()
{
    system("clear");
    file.clear();
    std::vector<std::string> title_list;
    title_list.push_back("Student Manage System");
    title_list.push_back("Student Information");
    file = readFile(STU_PATH);
    
    int max_[6] = {0};

    for (int i = 0; i < file.size(); i++)
    {
        for(int j = 0; j < file[i].size(); j++)
        {
            if(file[i][j].size() > max_[i]) max_[i] = file[i][j].size();
        } 
        max_[i] += 6;
    }

    int _max = 1;
    for(int i = 0; i < 6; i++) _max += max_[i] + 1;

    for (int i = 0; i < title_list.size(); i++)
    {

        for (int j = 0; j < _max; j++)
            std::cout << "\033[01;35m-\033[0m";

        std::cout << std::endl;
        std::cout << "\033[01;35m|\033[0m";
        for (int j = 0; j < ((_max - 2) / 2) - (title_list[i].size() / 2); j++)
            std::cout << " ";
        std::cout << "\033[01;35m" << title_list[i] << "\033[0m";
        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list[i].size() - title_list[i].size() / 2); j++)
            std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }

    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;
    
    for (int i = 0; i < file[0].size(); i++)
    {
        for(int j = 0; j < file.size(); j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (file[j][i].size() / 2); k++) std::cout << " ";
            
            std::cout << "\033[01;32m" << file[j][i] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (file[j][i].size() - file[j][i].size() / 2); k++)
            std::cout << " ";

            
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;
        for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }
    return _max;
}

int Rootuser::drawSubject_Info()
{
    system("clear");
    std::vector<std::string> title_list;
    title_list.push_back("Subject Manage System");
    title_list.push_back("Subject Information");
    file = readFile(SUB_PATH);
    int max_[file.size()] = {0};

    for (int i = 0; i < file.size(); i++)
    {
        for(int j = 0; j < file[i].size(); j++)
        {
            if(file[i][j].size() > max_[i]) max_[i] = file[i][j].size();
        } 
        max_[i] += 6;
    }

    int _max = 0;
    for(int i = 0; i < file.size(); i++) _max += max_[i];
    _max += file.size() + 1;

    for (int i = 0; i < title_list.size(); i++)
    {

        for (int j = 0; j < _max; j++)
            std::cout << "\033[01;35m-\033[0m";

        std::cout << std::endl;
        std::cout << "\033[01;35m|\033[0m";
        for (int j = 0; j < ((_max - 2) / 2) - (title_list[i].size() / 2); j++)
            std::cout << " ";
        std::cout << "\033[01;35m" << title_list[i] << "\033[0m";
        for (int j = 0; j < ((_max - 2) - (_max - 2) / 2) - (title_list[i].size() - title_list[i].size() / 2); j++)
            std::cout << " ";
        std::cout << "\033[01;35m|\033[0m" << std::endl;
    }

    for (int i = 0; i < _max; i++) std::cout << "\033[01;35m-\033[0m";
    std::cout << std::endl;
    
    for (int i = 0; i < file[0].size(); i++)
    {
        for(int j = 0; j < file.size(); j++)
        {
            std::cout << "\033[01;35m|\033[0m";
            for (int k = 0; k < (max_[j] / 2) - (file[j][i].size() / 2); k++) std::cout << " ";
            
            std::cout << "\033[01;32m" << file[j][i] << "\033[0m";
            for (int k = 0; k < (max_[j] - max_[j] / 2) - (file[j][i].size() - file[j][i].size() / 2); k++)
            std::cout << " ";

            
        }
        std::cout << "\033[01;35m|\033[0m";
        std::cout << std::endl;
        for(int j = 0; j < _max; j++) std::cout << "\033[01;35m-\033[0m";
        std::cout << std::endl;
    }
    return _max;
}